package emulator.hardware.memory;

import java.util.HashMap;

import emulator.hardware.HwWord;
import emulator.hardware.bus.Bus;

public class SparseMemory implements Bus {

	HashMap<Integer,Byte> map = new HashMap<Integer,Byte>();
	UnmappedMemoryHandler handler = new UnmappedMemoryThrow();
	
	@Override
	public int read(int address) throws UnmappedMemoryException {
		if (map.containsKey(address))
			return ((int) map.get(address)) & 0xFF;
		if (handler != null)
			return (int) handler.read(new HwWord(address)).getNumber();
		return 0;
	}

	@Override
	public void write(int address, int data) {
		map.put(address, (byte) data);
	}

	public void setUnmappedHandler(UnmappedMemoryHandler new_handler) {
		handler = new_handler;
	}

}
